package org.rsbot;

import java.awt.Dimension;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.rsbot.bot.Bot;
import org.rsbot.gui.BotGUI;
import org.rsbot.gui.BotPanel;
import org.rsbot.log.LogOutputStream;
import org.rsbot.log.SystemConsoleHandler;
import org.rsbot.util.Extractor;

public class Application
{
  private static BotGUI gui;
  private static long startTime;

  public static void main(String[] paramArrayOfString)
    throws Exception
  {
    bootstrap();
    new Extractor(paramArrayOfString).run();
    gui = new BotGUI();
    gui.setVisible(true);
    gui.addBot();
    stats();
  }

  public static Bot getBot(Object paramObject)
  {
    return gui.getBot(paramObject);
  }

  public static Dimension getPanelSize()
  {
    return gui.getPanel().getSize();
  }

  private static void bootstrap() {
    Logger.getLogger("").setLevel(Level.ALL);
    Logger.getLogger("").addHandler(new SystemConsoleHandler());

    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
    {
      private final Logger log = Logger.getLogger("EXCEPTION");

      public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
        this.log.logp(Level.SEVERE, "EXCEPTION", "", "Unhandled exception in thread " + paramThread.getName() + ": ", paramThrowable);
      }
    });
    System.setErr(new PrintStream(new LogOutputStream(Logger.getLogger("STDERR"), Level.SEVERE), true));
  }

  private static void stats() {
    sync(true);
    Runtime.getRuntime().addShutdownHook(new Thread()
    {
      public void run() {
        Application.access$000(false);
      }
    });
  }

  private static void sync(boolean paramBoolean)
  {
    try
    {
      URL localURL = new URL("http://stats.powerbot.org/sync/");
      HttpURLConnection localHttpURLConnection = (HttpURLConnection)localURL.openConnection();
      localHttpURLConnection.setRequestMethod("GET");
      localHttpURLConnection.setDoOutput(true);
      localHttpURLConnection.setDoInput(true);
      localHttpURLConnection.setUseCaches(false);
      localHttpURLConnection.setAllowUserInteraction(false);
      StringBuilder localStringBuilder = new StringBuilder("s=");
      if (paramBoolean) {
        localStringBuilder.append("1");
        startTime = System.currentTimeMillis();
      } else {
        localStringBuilder.append("0&t=").append((System.currentTimeMillis() - startTime) / 60000L);
      }
      OutputStreamWriter localOutputStreamWriter = new OutputStreamWriter(localHttpURLConnection.getOutputStream(), "UTF-8");
      localOutputStreamWriter.write(localStringBuilder.toString());
      localOutputStreamWriter.flush();
      localOutputStreamWriter.close();
      localHttpURLConnection.disconnect();
    }
    catch (IOException localIOException)
    {
    }
  }
}